/*
 * 太原科技大学。
 * Copyright 2021, YCHS Information&Science Technology Co.,Ltd,
 * All rights reserved.
 */
package com.example.demo.Utils;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author liuchao
 * @version 1.0
 * @date 2024/9/15
 */

@Component
public class AliyunSmsSender {
    @Autowired
    AliyunConfigRead aliyunConfigRead;

    //验证码发送
    public SendSmsResponse smsSend(String phone, String code) throws Exception {
        //读取阿里云配置信息
        String accessKeyId = aliyunConfigRead.getAccessKeyId();
        String accessKeySecret = aliyunConfigRead.getAccessKeySecret();
        String endpoint = aliyunConfigRead.getEndpoint();
        //由于阿里的singName必须要中文，但是我读取的配置文件中文是乱码，作者很无奈，这里作者直接赋值了
        String singName = "待办App";

        String templateCode = aliyunConfigRead.getTemplateCode();
        //配置服务器连接
        Config config = new Config().setAccessKeyId(accessKeyId).setAccessKeySecret(accessKeySecret);
        config.endpoint = endpoint;
        //信息配置及发送
        Client client = new Client(config);
        SendSmsRequest sendSmsRequest=new SendSmsRequest()
                .setSignName(singName)
                .setTemplateCode(templateCode)
                .setPhoneNumbers(phone)
                .setTemplateParam("{\"code\":\"" + code + "\"}");

        return client.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions());
    }
}